@file:Suppress(
        "UNUSED_VARIABLE",
        "unused",
        "VARIABLE_WITH_REDUNDANT_INITIALIZER",
        "UNUSED_DESTRUCTURED_PARAMETER_ENTRY",
        "UNUSED_EXPRESSION",
        "UNUSED_PARAMETER",
        "UNUSED_ANONYMOUS_PARAMETER"
)

package ht

// 注释
/*
ver: 
*/

/**
 * 文档
 */
fun mute(vararg a:Any) {
}

/**
 * Created by ht
 */
object EasyKotlin {
    fun vari() {
        println("变量常量")
        var a = 1
        val b = 1L
        var c: Int? = 1
        val f = 0.1f
        val x = if (a == 1) 1 else 0

        // ?.意思是这个参数可以为空,并且程序继续运行下去
        // !!.的意思是这个参数如果为空,就抛出异常

        a = 2
        //b=2
        c = null
        //a = null

        a.toString()
        b
        c?.toString()
        f.toInt()

    }

    fun stri() {
        println("字符串")
        val s = "a\nb"
        val t = "$s has ${s.length} characters"
        val sub = s.substring(0,2)
        val lines = """
            a
            b
        """.trimIndent()

        t.length
        lines.length
    }

    fun coll() {
        println("数组集合")
        val arr = arrayOf("1", 2, 3)
        val list1 = listOf(1, 2, "3")
        val list2 = mutableListOf(1, 2, "3")
        val set1 = setOf(1, 2, 3)
        val map1 = mapOf("1" to 1, "2" to 2)

        arr[0]
        arr.toList()
        arr.toMutableList()
        list2.add(4)
        list2.remove(4)

        list1.forEach {
            it.toString().toInt() + 1
        }
        list1.withIndex().forEach { (i, e) ->
        }
        map1.forEach { key, value ->
        }

        //range
        val i = 3
        if ( i in 1..3) { 
            mute(i) 
        }
    }

    fun proc() {
        println("流程控制")
        var a = 1
        if (a == 1) {
            a = 2
            a
        }

        val obj: Any = "a"
        if (obj is String) {
            obj.length
        }

        when (obj) {
            1 -> obj == 1
            "1" -> obj == "1"
            is Long -> obj.toInt()
            else -> obj.toString()
        }
    }

    fun func() {
        println("函数")
        val add = fun(a: Int, b: Int): Int {
            return a + b
        }
        add(1, 2)

        val add2 = fun(a:Int, b:()->Int):Int { //Unit
            return a+b()
        }
        add2(1,{->2})
    }

    fun clas() {
        println("类和对象")
        class A
        data class B(val b1: String, val b2: String)

        val b = B("b1", "b2")
        val (_, b2) = b

        val a = A()
    }

    fun othe() {
        println("其它")
        val p: String by lazy {
            "a" + "b"
        }

        //流式编程
        val names = arrayOf("a")
        names.filter { it.startsWith("A") }
                .filter { x -> x.startsWith("A") }
                .sortedBy { it }
                .map { it.toUpperCase() }
                .forEach { println(it) }


        //为String类扩展方法
        fun String.spaceToCamelCase() {
        }

        p

        //try-with-resource
        // OutputStreamWriter(r.getOutputStream()).use {
            // it.write('a')
        // }
    }

    fun erro() {
        println("错误异常")
        try {
            val arg1 ="ai".toInt()
            println("$arg1")
        } catch (e: NumberFormatException) {
            // e.printStackTrace()
            mute("您确定输入的是整数吗？")
        } catch (e: Exception) {
            println("${e.message}")
        } finally {
            mute("")
        }
    }

    fun file() {

    }

    fun net() {

    }
}

class A {
    companion object {
        const val a = "a0"
    }

    val a = "a1"
}

open class B {
    open fun b0() {}
    fun b1() {}
}

class Bb constructor(var bb: String) : B() {
    init {
    }

    val customerKey = ""

    val isEmpty: Boolean
        get() = this.bb.isEmpty()

    var like: String
        get() = this.toString()
        set(value) {
        }

    class C {
        var c = "c"
    }

    inner class Inner {
        fun f() = bb
        fun t() {
            println(bb)
            val o = this@Bb
            println(o.bb)
        }
    }

    override fun b0() {
        val c = C()
        c.c = "bc"
    }
}

interface I {
    val property: Int
    fun f1()
    fun f2() {
    }
}

enum class Direction {
    NORTH, SOUTH, WEST, EAST
}


fun main(args: Array<String>) {
    println("Hello, World!")
    EasyKotlin.vari()
    EasyKotlin.stri()
    EasyKotlin.coll()
    EasyKotlin.proc()
    EasyKotlin.func()
    EasyKotlin.clas()
    EasyKotlin.erro()
    EasyKotlin.othe()
}